from django import forms
from grades.models import Grade
from teachers.models import Teacher


GENDER_CHOICES=[
    ('男', '男'),
    ('女', '女'),
]

class TeacherForm(forms.ModelForm):
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['grade'].queryset = Grade.objects.all()
        self.fields['grade'].empty_label = '请选择班级'

    def clean_phone_number(self):
        print(f'self.instance is {self.instance}')
        phone_number = self.cleaned_data.get('phone_number')
        if Teacher.objects.filter(phone_number=phone_number).exclude(pk=self.instance.pk).exists():
            raise forms.ValidationError('具有该手机号的教师信息已存在!')
        return phone_number
    
    def clean_grade(self):
        grade = self.cleaned_data.get('grade')
        if Teacher.objects.filter(grade=grade).exclude(pk=self.instance.pk).exists():
            raise forms.ValidationError('具有该管理班级的教师信息已存在!')
        return grade
    
    class Meta:
        model = Teacher
        fields = ['grade','teacher_name','gender','phone_number','birthday']